home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4761 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  82 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Comparing Struct elements
  5. Date: 1 Feb 1996 00:02:51 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4eovvb$jv8@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe7.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 31, 1996 15:30:50 in article <Comparing Struct elements>,
  15. 'bhardwaj@ccs.neu.edu (saurabh bhardwaj)' wrote: 
  16.  
  17.  
  18. >Hello, 
  19. >I've been trying to compare structure elements using the if Statement. 
  20. Here 
  21. >is the code: 
  22. >double NumericVal(ClassRec& LetGrade){ 
  23. >cout << "assgning struct member usin . " << LetGrade. 
  24. >Grade << '\n'; 
  25. >if(LetGrade.Grade == "A") return(4.000); 
  26. >else if(LetGrade.Grade == "A-") return(3.666); 
  27. >else if(LetGrade.Grade == "B+") return(3.333); 
  28. >else return 0; 
  29. >} 
  30. >where LetGrade is the following struct: 
  31. >struct ClassRec { 
  32. >char Grade[3]; 
  33. >int   QH; 
  34. >char Name[10]; 
  35. >}; 
  36. >I can't seem to compare the elements of the Grade[3] with "A" or "A-" or
  37. "B+" 
  38. >The function invariable returns 0.  I've also tried using the -> operator.
  39.  It 
  40. >doesn't work either!!!   Anyone know why???  I'll appreciate any help. 
  41. You have three choices -- well, actually it's only two. 
  42.  
  43. 1.  Use strcmp() instead ==.  When you code (foo == "foo"), you are 
  44. actually comparing the addresses of the strings.  They're never equal. 
  45.  
  46. 2.  Define operator== for your struct that takes a pointer to a character 
  47. array as an argument.  Of course, inside this operator, you should use 
  48. strcmp, so this solution is almost like 1. 
  49.  
  50. 3.  Use a string class instead of raw strings.  If using MFC, for example, 
  51. you could change your structure definition to something like 
  52.  
  53. struct ClassRec  
  54.  { 
  55.    CString Grade; 
  56.    int QH; 
  57.    CString Name; 
  58.  }; 
  59.  
  60. On a "real" program, solution #3 is the best one.  On a school project, 
  61. take your pick. 
  62.  
  63. Anyway, the bottom line here is that you can't compare complex  
  64. structures using the "==" unless you have defined an operator 
  65. for them.  A string is not a simple type as it is really an array of 
  66. characters.  Consequently, you must use some other means  
  67. of comparing the equivalence of a pair of strings.  The standard 
  68. C library function strcmp() is just such a tool. 
  69.  
  70. -- 
  71. Pete Grant 
  72. Kalevi, Inc. 
  73. Object Oriented Software Development
  74.